﻿Public Class FormType

    Private Sub FormType_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(Me.dgv)
        LoadData()
    End Sub
    Private Sub LoadData()
        Cmd.Parameters("@s") = "%" & txtSearch.Text & "%"
        Me.dgv.DataSource = Cmd.ExecuteDataTable("SELECT * FROM Type WHERE Active=1 AND UPPER(TypeName) LIKE UPPER ('%'+@s+'%')")
    End Sub

    Private Sub txtSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSearch.TextChanged
        LoadData()
    End Sub

    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click

        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If

        If MessageBox.Show("Are you sure want to delete ?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question) <> Windows.Forms.DialogResult.Yes Then
            Exit Sub
        End If
        Dim row = Me.dgv.SelectedRows(0)
        Dim id = row.Cells("TypeId").Value


        Cmd.Parameters("@Typeld") = id
        Cmd.ExecuteNonQuery("UPDATE Type SET Active=0 WHERE TypeId=@Typeld")

        LoadData()
    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
        Dim frm As New FormTypeEditor()
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        If Me.dgv.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = Me.dgv.SelectedRows(0)
        Dim frm As New FormTypeEditor()
        frm.txtTypId.Text = row.Cells("TypeId").Value
        frm.txtTypeName.Text = row.Cells("TypeName").Value
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            LoadData()
        End If

    End Sub
End Class